package cn.myzf.fort.access.accesscommon.processoutput.reader;


import cn.myzf.fort.access.accesscommon.processoutput.core.StreamingProcessOutputType;
import cn.myzf.fort.access.accesscommon.processoutput.core.StreamingProcessOwner;

public class StreamingProcessReader
        extends AbstractProcessReader {

    /** the owner. */
    protected StreamingProcessOwner m_Owner;

    /** whether to forward the output to the owner. */
    protected boolean m_Forward;

    /**
     * Initializes the reader.
     *
     * @param owner the owning object
     * @param process the process to monitor
     * @param stdout  whether to read stdout or stderr
     */
    public StreamingProcessReader(StreamingProcessOwner owner, Process process, boolean stdout) {
        super(process, stdout);
        m_Owner = owner;
        m_Forward = (stdout && (m_Owner.getOutputType() == StreamingProcessOutputType.STDOUT))
                || (!stdout && (m_Owner.getOutputType() == StreamingProcessOutputType.STDERR))
                || (m_Owner.getOutputType() == StreamingProcessOutputType.BOTH);
    }

    /**
     * For processing the line read from stdout/stderr.
     *
     * @param line	the output line
     */
    @Override
    protected void process(String line) {
        if (m_Forward)
            m_Owner.processOutput(line);
    }
}